import storage from 'store'
import expirePlugin from 'store/plugins/expire'
import {
  getAreaList,
  fetchFirmSearch
} from '@/api/informanage'
import {
  fetchOptions
 } from '@/api/casemanage'

import {
  fetchCtypeSearch
} from '@/api/accountmanage'

storage.addPlugin(expirePlugin)
const collect = {
  state: {
    areaList: [],
    areaListLevel: [],
    ctypeList: []
  },

  mutations: {
    SET_AREA: (state, areaList) => {
      state.areaList = areaList
    },

    SET_AREA_LEVEL: (state, areaList) => {
      state.areaListLevel = areaList
    },

    SET_CTYPE: (state, ctypeList) => {
      state.ctypeList = ctypeList
    },

    SET_FIRM: (state, firmList) => {
      state.firmList = firmList
    },

    SET_OPTIONS: (state, options) => {
      state.options = options
    }
  },

  actions: {
    // 获取地区
    GetAreaList ({ commit }, data) {
      return new Promise((resolve, reject) => {
        getAreaList(data).then(response => {
          const result = response.data
          commit('SET_AREA', result.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetAreaListLevel ({ commit }, data) {
      return new Promise((resolve, reject) => {
        getAreaList(data).then(response => {
          const result = response.data
          commit('SET_AREA_LEVEL', result.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetCtypeList ({ commit }, data) {
      return new Promise((resolve, reject) => {
        fetchCtypeSearch(data).then(response => {
          const result = response
          commit('SET_CTYPE', result.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetOptions ({ commit }, data) {
      return new Promise((resolve, reject) => {
        fetchOptions(data).then(response => {
          const result = response.data
          commit('SET_OPTIONS', result)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetFirmList ({ commit }, data) {
      return new Promise((resolve, reject) => {
        fetchFirmSearch(data).then(response => {
          const result = response.data
          commit('SET_FIRM', result.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default collect
